import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StartScreen extends JFrame {
	//start the game
    JButton startButton;
    
    //reference to game object (the parent)
    Game game;
    
    //exit the game
    JButton exitButton;
    
    //enter number of dots for the height
    JTextField heightTextField;
    
    //enter number of dots for the width
    JTextField widthTextField;
    
    //player or computer go first
    JComboBox goFirst;
    
    /**
     * Return the width value the user entered. If the user didn't enter, return 0
     */
    public int getBoardWidth()
    {
        return widthTextField.getText().equals("")?0:Integer.parseInt(widthTextField.getText());
    }
    
    /**
     * Return the height value the user entered. If the user didn't enter, return 0
     */
    public int getBoardHeight()
    {
        return heightTextField.getText().equals("")?0:Integer.parseInt(heightTextField.getText());
    }
    
    public StartScreen(Game game)
    {
    	setSize(800, 600);
        setLayout(new FlowLayout());
        startButton = new JButton("Start");
        startButton.addActionListener(new StartButtonHandler());
        exitButton = new JButton("Exit");
        exitButton.addActionListener(new ExitButtonHandler());
        heightTextField = new JTextField(5);
        widthTextField = new JTextField(5);
        String[] goList = {"Player", "Computer"};
        goFirst = new JComboBox(goList);
        this.game = game;
        
        //add the component to the screen
        add(new JLabel("Enter The Width of the board (in dots): "));
        add(widthTextField);
        add(new JLabel("Enter The height of the board (in dots): "));
        add(heightTextField);
        add(new JLabel("Who go first: "));
        add(goFirst);
        add(startButton);
        add(exitButton);
    }
    
    public void initScreen()
    {
            heightTextField.setText("");
            widthTextField.setText("");
            goFirst.setSelectedIndex(0);
    }
    
    private class ExitButtonHandler implements ActionListener
    {
            @Override
            public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    if (game.getGameScreen() != null)
                    	game.getGameScreen().dispose();
                    game.getStartScreen().dispose();
            }
    }
    
    private class StartButtonHandler implements ActionListener
    {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			game.displayGameScreen();
		}
    }
    
    /**
     * Whether the player or computer go first
     * @return 1 if player go first, -1 if computer go first
     */
    public int getGoFirst()
    {
    	return goFirst.getSelectedIndex() == 0 ? 1 : -1;
    }
}
